import 'package:flutter/material.dart';

import '../global_config/constants.dart';

class CustomForm extends StatefulWidget {
  const CustomForm({Key? key}) : super(key: key);

  @override
  State<CustomForm> createState() => _CustomFormState();
}

class _CustomFormState extends State<CustomForm> {
  GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Form(
        // onWillPop: () => _willPop(context),
        key: _formKey,
        onChanged: () {
          print('Form---onChanged');
        },
        child: Stack(
          alignment: Alignment.centerRight,
          children: <Widget>[
            Container(
              width: 350,
              child: UnconstrainedBox(
                child: Container(
                  width: 200,
                  height: 70,
                  child: TextFormField(
                    style: TextStyle(textBaseline: TextBaseline.alphabetic),
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'username',
                    ),
                    validator: _validateUsername,
                  ),
                ),
              ),
            ),
            Positioned(top: 0, right: 0, child: _buildSubmitButton(context)),
          ],
        ),
      ),
    );
  }

  String? _validateUsername(value) {
    if (value.isEmpty) {
      return '用户名不能为空';
    }
    return null;
  }

  ElevatedButton _buildSubmitButton(BuildContext context) {
    return ElevatedButton(
      // color: Colors.blue,
      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.all<Color>(Color(0xFFDFDFDF)),
        shape: MaterialStateProperty.all<RoundedRectangleBorder>(
          RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(6.0),
            side: BorderSide(
              color: Theme.of(context).primaryColor,
            ),
          ),
        ),
      ),
      onPressed: _onSubmit,
      child: Icon(
        Icons.check,
        color: Colors.white,
      ),
    );
  }

  _onSubmit() {
    if (_formKey.currentState!.validate()) {
      FocusScope.of(context).requestFocus(FocusNode());
      Navigator.of(context).pop();
      Navigator.pushNamed(globalNavigatorKey.currentContext!, '/Home');
    }
  }

  // Future<bool> _willPop(context) async {
  //   return await showDialog(
  //         context: context,
  //         builder: (context) => AlertDialog(
  //           shape: RoundedRectangleBorder(
  //               borderRadius: BorderRadius.all(Radius.circular(10))),
  //           title: Text('提示'),
  //           content: Text('你确定要离开此页吗?'),
  //           actions: <Widget>[
  //             TextButton(
  //               onPressed: () => Navigator.of(context).pop(true),
  //               child: Text('确定'),
  //             ),
  //             TextButton(
  //               onPressed: () => Navigator.of(context).pop(false),
  //               child: Text('取消'),
  //             ),
  //           ],
  //         ),
  //       ) ??
  //       false;
  // }
}
